import { _decorator, Component, Node } from 'cc';
import { WECHAT } from 'cc/env';
import { Global } from '../Global';
const { ccclass, property } = _decorator;

@ccclass('WxPlatform')
export class WxPlatform {
    static _ins: WxPlatform;
    public wx = window["wx"];
    static get ins() {
        if (!this._ins) {
            this._ins = new WxPlatform();
        }
        return this._ins;
    }

    wechat_setting() {
        console.log("1 执行 wechat_setting", WECHAT);
        if (!WECHAT) {
            return;
        }
        console.log("2 执行 wechat_setting");
        this.wx.showShareMenu({
            withShareTicket: true,
            menus: ['shareAppMessage', 'shareTimeline']
        });
        //upgrade 
        const updateManager = this.wx.getUpdateManager()
        updateManager.onCheckForUpdate(function (res) {
            console.log("请求完新版本信息的回调", res.hasUpdate);
        })
        updateManager.onUpdateReady(function () {
            this.wx.showModal({
                title: '更新提示',
                content: '新版本已经准备好，是否重启应用？',
                success(res) {
                    if (res.confirm) {
                        // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                        updateManager.applyUpdate()
                    }
                }
            })
        })
        updateManager.onUpdateFailed(function () {
            console.log("新版本下载失败")
        })
    }

    show_interstitial(delay = 0) {
        console.log("显示差评广告》〉》〉》〉》〉》〉》〉》");
        if (!WECHAT) {
            return;
        }
        let ad_video_id = Global.ad_intersitial_id;
        let interstitialAd = this.wx.createInterstitialAd({
            adUnitId: ad_video_id.trim(),
        });
        if (interstitialAd) {
            interstitialAd.load().then(() => {
                console.log('插屏广告加载成功');
            });
            interstitialAd.onClose(res => {
                console.log('关闭插屏广告', res);
            });
            interstitialAd.onError(err => {
                console.log('err:插屏广告加载失败', err);
            });

            setTimeout(() => {
                interstitialAd.show().catch((err) => {
                    console.log('插屏广告展示失败', err);
                });
            }, delay * 1000);
        }
    }

    rewardVideo: any;
    show_reward_video(callback: Function) {
        let my = this;
        let ad_video_id = Global.ad_video_id;
        if (my.rewardVideo != null) {
            my.rewardVideo.offClose(fun);
        }
        let rewardedVideoAd = this.wx.createRewardedVideoAd({
            adUnitId: ad_video_id.trim(),
        });
        my.rewardVideo = rewardedVideoAd;
        rewardedVideoAd.load().then(() => {
            this.wx.showToast({
                title: "看完视频领奖励",
                icon: 'success',
                duration: 1500,
                mask: false,
                success: function () { },
                fail: function () { },
                complete: function () { }

            })
            console.log('视频 广告加载成功');
            rewardedVideoAd.show();
        });
        rewardedVideoAd.onError(err => {
            console.log('激励视频 广告显示失败', err);
            this.wx.showToast({
                title: "请稍后再试",
                icon: 'fail',
                duration: 1500,
                mask: false,
                success: function () { },
                fail: function () { },
                complete: function () { }
            })
            callback(2);
        })
        var fun = function (res) {
            if (res && res.isEnded) {
                console.log('res:  ', res);
                callback(1);
                rewardedVideoAd.offClose(fun);
            } else {
                console.log('播放中退出');
                callback(0);
            }
        }
        rewardedVideoAd.onClose(fun);
    }


    banner: any;
    banner_show_status:boolean = false;
    hide_banner() {
        this.banner_show_status = false;
        if (this.banner != null) {
            this.banner.destroy();
            this.banner = null;
            console.log('关闭 banner...');
        }
    }

    show_banner(style = 2) {
        if (!WECHAT) {
            return;
        }
        if (this.banner != null) {
            this.banner.destroy();
            this.banner = null;
            return;
        }
        this.banner_show_status = true;

        let ad_banner_id = Global.ad_banner_id;
        console.log('showBanner start ad_banner_id::',ad_banner_id);
        let winSize = this.wx.getSystemInfoSync();
        let bannerAd = this.wx.createBannerAd({
            adUnitId: ad_banner_id,
            adIntervals: 30,
            style: {
                left: 0,
                top: 500,
                width: 300,
            }
        })
        let left;
        if (style == 1) {
            left = winSize.windowWidth - bannerAd.style.realWidth;
        }
        else if (style == 2) {
            left = winSize.windowWidth / 2 - bannerAd.style.realWidth / 2;
        }
        else {
            left = 0;
        }
        bannerAd.onResize(res => {
            bannerAd.style.top = winSize.windowHeight - bannerAd.style.realHeight;
            bannerAd.style.left = left;
        })
        bannerAd.onLoad(() => {
            console.log('banner 广告加载成功');
            this.banner = bannerAd;
            if(!this.banner_show_status){
                //不应该显示，需要隐藏
                this.hide_banner();
                console.log('错误出发，关闭bannner');
                // this.wx.showToast({
                //     title: "错误出发，关闭bannner",
                //     icon: 'success',
                //     duration: 1500,
                //     mask: false,
                //     success: function () { },
                //     fail: function () { },
                //     complete: function () { }
    
                // })
            }
        })
        bannerAd.onError(err => {
            console.log('banner 广告加载错误', err);
        })
        bannerAd.show();
    }
}

